<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      <el-input v-model="form.id" v-show="false" v-bind:disabled="true"></el-input>
      <el-form-item label="用户名">
        <el-input v-model="form.userName" ></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="等级">
        <el-select v-model="form.roleId" placeholder="请选择用户等级">
          <template v-for="role in roles">
            <el-option :label="role.name" :value="role.id"></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="权限">
        <el-checkbox-group v-model="form.Allpermissions">
          <template v-for="Allpermissions in Allpermissions">
              <el-checkbox :checked="Allpermissions.checked" :label="Allpermissions.name"></el-checkbox>
          </template>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">Create</el-button>
        <el-button @click="onCancel">Cancel</el-button>
        <el-button type="primary" @click="onSubmit2">保存权限</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { getRoleListAll, getAllpermissions } from '@/api/role'
  import { editUser, getUser, getUserpermissions, postUserpermissions } from '@/api/user'

  export default {
    data() {
      return {
        form: {
          id: 0,
          userName: '',
          password: '',
          roleId: '',
          Allpermissions: []
        },
        roles: {},
        Allpermissions: null,
        Userpermissions: null
      }
    },
    created() {
      getRoleListAll().then(response => {
        this.roles = response.data.content
      })
      this.form.id = this.$route.query.id
      getUser(this.form.id).then(response => {
        this.form.userName = response.data.userName
        this.form.roleId = response.data.roleId
      })
      getAllpermissions().then(response => {
        var Allpermissions = response.data.content
        getUserpermissions(this.form.id).then(response => {
          this.Allpermissions = Allpermissions
          this.Userpermissions = response.data
          for (let i = 0; i < this.Allpermissions.length; i++) {
            for (let j = 0; j < this.Userpermissions.length; j++) {
              this.Allpermissions[i].checked = false
              if (this.Allpermissions[i].name === this.Userpermissions[j].name) {
                this.Allpermissions[i].checked = true
              }
            }
          }
          console.log(this.Allpermissions[1].checked)
        })
      })
    },
    methods: {
      onSubmit() {
        editUser(this.form.id, this.form.password, this.form.roleId).then(response => {
          if (response.code === 1) {
            this.$message('修改成功!')
            this.$router.push({ path: '/sys/user' })
          } else {
            this.$message({
              message: response.msg,
              type: 'warning'
            })
          }
        })
      },
      onCancel() {
        this.$router.push({ path: '/sys/user' })
      },
      onSubmit2() {
        var string = ''
        for (let i = 0; i < this.form.Allpermissions.length; i++) {
          string = this.form.Allpermissions[i] + ',' + string
        }
        console.log(string)
        string = string.slice(0, string.length - 1)
        postUserpermissions(this.$route.query.id, string).then(response => {
          console.log(response)
        })
      }
    }
  }
</script>

<style scoped>
  .line {
    text-align: center;
  }
</style>

